import { menu } from '../../decorator';
import colorShapRing from '../render/colorShapRing';

const { ccclass, property, requireComponent } = cc._decorator;

@ccclass
@menu(__filename)
@requireComponent(colorShapRing)
export default class ringAction extends cc.Component {
    @property
    speed = 0.02;

    private _speed = 0;
    private _progress = 0;
    private _colorRing: colorShapRing = null;

    onLoad() {
        this._colorRing = this.getComponent(colorShapRing)
    }

    onEnable() {
        this._progress = 0;
        this._speed = this.speed;
        this._colorRing.progress = 0;
        this._colorRing.reverse = false;
    }

    update() {
        if (this._speed > 0) {
            this._progress = Math.min(1, this._progress + this._speed);
            this._colorRing.progress = this._progress;

            if (this._progress == 1) {
                this._speed = -this._speed;
                this._colorRing.reverse = !this._colorRing.reverse;
            }
        } else {
            this._progress = Math.max(0, this._progress + this._speed);
            this._colorRing.progress = this._progress;

            if (this._progress == 0) {
                this._speed = -this._speed;
                this._colorRing.reverse = !this._colorRing.reverse;
            }
        }
    }
}